package com.supermap.dataservice.agrinfo;

import com.supermap.dataservice.agrinfo.dao.AgrinfoDao;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.agrinfo.entity.AgrAllInfo;
import com.supermap.dubbo.api.agrinfo.entity.Agrinfo;
import com.supermap.dubbo.api.agrinfo.entity.AgrinfoComment;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.agrinfo.AgrinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:langhongshuai
 * @Description:农情业务逻辑层
 * @Date:18/1/29 下午3:01
 */
@Service(timeout = 10000)
public class AgrinfoServiceImpl implements AgrinfoService {
    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(AgrinfoServiceImpl.class);

    @Resource
    private AgrinfoDao agrinfoDao;

    @Override
    public JSONUtil getAgrinfoByUIdArea(String userId,String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得用户所有的农情信息
            if(areaCode!=null){
                areaCode=areaCode+"%";
            }
            List<Agrinfo> agrinfos = agrinfoDao.getAgrinfoByUIdArea(areaCode);
            for(Agrinfo agr:agrinfos){
                int agrId=agr.getId();
                String isPraise = agrinfoDao.isPraise(userId, agrId);
                if(isPraise==null || isPraise.equals("0")){
                    agr.setIsPraise(0);//未点赞
                }else{
                    agr.setIsPraise(1);//已点赞
                }
            }

            //获得用户所有的农情信息后返回的json数据
            jsonUtil.setMsg("Get user agrinfos success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agrinfos);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>getAgrinfoByUserId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get user agrinfos flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }


    @Override
    public JSONUtil addAgrinfo(Agrinfo agrinfo) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //添加农情信息
           agrinfoDao.addAgrinfo(agrinfo);
         Integer agrId=agrinfoDao.getInsertAgrinfoId();
         //添加图片
            if(agrinfo.getImgUrl()!=null && !agrinfo.getImgUrl().equals("")){
             String str[]=agrinfo.getImgUrl().substring(1,agrinfo.getImgUrl().length()-1).split(",");
                for (String s:str){
                    agrinfoDao.addAgrinfoImg(agrId,s);
                }
            }
            agrinfo.setId(agrId);
            //添加农情信息后返回的json数据
            jsonUtil.setMsg("add user agrinfos success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agrinfo);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>addAgrinfo=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("add user agrinfos flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }



    @Override
    public JSONUtil getAgriMsgCountByArea(String[] areaCodes, String year) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Agrinfo> agrinfos = agrinfoDao.getAgriMsgCountByArea(areaCodes, year);

            jsonUtil.setMsg("add user agrinfo success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agrinfos);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>addAgrinfoOrDisasterInfo=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("add user agrinfo flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAllAgrinfo() {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得用户所有的农情信息
             List<Agrinfo> agrinfoList = agrinfoDao.getAllAgrinfo();
            //获得用户所有的农情评论信息
             List<AgrinfoComment> agrinfoComments = agrinfoDao.getAllAgrinfoComment();
            for (Agrinfo agrinfo:agrinfoList) {
                List<AgrinfoComment> newAgrinfoComments = new ArrayList<>();
                for(AgrinfoComment agrinfoComment:agrinfoComments){
                    if(agrinfo.getId()==agrinfoComment.getAgrInfoId()){
                        newAgrinfoComments.add(agrinfoComment);
                    }
                }
                agrinfo.setAgrinfoComments(newAgrinfoComments);
            }
            //获得用户所有的农情信息后返回的json数据
            jsonUtil.setMsg("Get all agrinfos success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agrinfoList);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>getAllAgrinfo=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get all agrinfos flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public boolean addagrinfocomment(AgrinfoComment agrinfoComment,String uid) {
        int a=agrinfoDao.addagrinfocomment(agrinfoComment);
        if(a==1){
           //agrinfoDao.addNewMessage(agrinfoComment.getUserId(),agrinfoComment.getAgrInfoId(),2,agrinfoComment.getComment(),0,uid);
            return true;
        }
        return false;
    }

    @Override
    public Integer getLastId() {
        return agrinfoDao.getLastId();
    }

    @Override
    public List<AgrinfoComment> getAllAgrinfoCommentByagrInfoId(int agrInfoId) {
        return agrinfoDao.getAllAgrinfoCommentByagrInfoId(agrInfoId);
    }

    @Override
    public JSONUtil showAgrinfoComment(List<AgrinfoComment> list) {
        List<AgrinfoComment> agrinfoComments = new ArrayList<AgrinfoComment>();
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            for (AgrinfoComment a:list) {
                if(a.getParentId()==-1){
                    agrinfoComments.add(a);
                }
            }
            getAgrinfoCommentList(list, agrinfoComments);
            int AgrCount=list.size();
            jsonUtil.setMsg("AgrinfoCommentCount:"+AgrCount);
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agrinfoComments);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>addAgrinfoOrDisasterInfo=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("get showAgrinfoComment flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }

        return jsonUtil;
    }

    @Override
    public JSONUtil delAgrinfo(int agrInfoId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            agrinfoDao.delAgrinfo(agrInfoId);

            jsonUtil.setMsg("del user agrinfo success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agrInfoId);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>addAgrinfoOrDisasterInfo=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("del user agrinfo flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAgrinfoByAreacodeandCropid(String areacode,String type) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Agrinfo> agr = agrinfoDao.getAgrinfoByAreacodeandCropid(areacode, type);
            jsonUtil.setMsg("getAgrinfoByAreacodeandCropid success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agr);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>getAgrinfoByAreacodeandCropid=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getAgrinfoByAreacodeandCropid flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAgrinfos(String type,String areaCode,String dataTime) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Agrinfo> agr = agrinfoDao.getAgrinfos(type,areaCode,dataTime);
            jsonUtil.setMsg("getAgrinfoByCropId success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agr);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>getAgrinfoByCropId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getAgrinfoByCropId flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    //递归(根据ParentId和对应的ID得到List)
    private void getAgrinfoCommentList(List<AgrinfoComment> list, List<AgrinfoComment> agrinfoComments) {
        if(agrinfoComments.size()!=0) {
            for (AgrinfoComment a : agrinfoComments) {
                List<AgrinfoComment> agrinfoCommentList = new ArrayList<>();
                for (AgrinfoComment b : list) {
                    if (b.getParentId() == a.getId()) {
                        agrinfoCommentList.add(b);
                    }
                }
                getAgrinfoCommentList(list, agrinfoCommentList);
                a.setChildrenNode(agrinfoCommentList);
            }
        }
    }

    @Override
    public JSONUtil searchAgrInfoByShowName(String showName, String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Agrinfo> agr = agrinfoDao.searchAgrInfoByShowName(showName,areaCode);
            jsonUtil.setMsg("searchAgrInfoByShowName success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(agr);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>searchAgrInfoByShowName=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("searchAgrInfoByShowName flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAgrAllInfoByUserIdorAreaCode(Table table, String userId,String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        List<AgrAllInfo> agrAllList=new ArrayList<>();
         int agrCount;
        try{
            if(areaCode!=null){
                //获取所有农情
                areaCode=areaCode+"%";
                agrAllList = agrinfoDao.getAgrAllInfoByAreaCode(table,areaCode);
                agrCount=agrinfoDao.getAgrCountByAreaCode(areaCode);
            }else {
                //获取个人发布的所有农情
                agrAllList = agrinfoDao.getAgrAllInfoByUserId(table, userId);
                agrCount = agrinfoDao.getAgrCountUserId(userId);
            }
            if(agrAllList.size()>0){
                for(AgrAllInfo agrAllInfo:agrAllList){
                    Integer agrId = agrAllInfo.getId();
                    //该农情的点赞总数
                    int praiseNums=agrinfoDao.getAgrPraise(agrId);
                    agrAllInfo.setPraiseNums(praiseNums);
                    //登陆者是否对该农情点赞
                    String isPraise =agrinfoDao.isPraise(userId,agrId);
                    if(isPraise==null || isPraise.equals("0")){
                        agrAllInfo.setIsPraise(0);//未点赞
                    }else{
                        agrAllInfo.setIsPraise(1);//已点赞
                    }
                    //查出该农情的评论内容
                    List<AgrinfoComment> commentList = agrAllInfo.getAgrComments();
                    List<AgrinfoComment> agrCommentList = agrinfoDao.getAgrComment(agrId);
                    for(AgrinfoComment agr:agrCommentList){
                        if(agr.getParentId()==-1)commentList.add(agr);
                    }
                    getAgrinfoCommentList(agrCommentList,commentList);

                }
            }
            table.setCount(agrCount);
            table.setData(agrAllList);
            jsonUtil.setMsg("getAgrAllInfoByUserId  success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("AgrinfoServiceImpl=>getAgrAllInfoByUserId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getAgrAllInfoByUserId flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil agriInfoCount(Table table, Agrinfo agrinfo) {
        JSONUtil jsonUtil = new JSONUtil();
        if(agrinfo.getAreacode()!=null&&!"".equals(agrinfo.getAreacode())){
            List<Agrinfo> list = agrinfoDao.agriInfoCount(table,agrinfo);
           int count = agrinfoDao.countCity(agrinfo.getAreacode());
            table.setData(list);
            table.setCount(count);
            jsonUtil.setMsg("成功！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        }else{
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
            jsonUtil.setMsg("区域不能为空！");
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil farmCount(Table table, Agrinfo agrinfo) {

        JSONUtil jsonUtil = new JSONUtil();
        if(agrinfo.getAreacode()!=null&&!"".equals(agrinfo.getAreacode())){
            List<Agrinfo> list = agrinfoDao.farmCount(table,agrinfo);
            int count = agrinfoDao.countCity(agrinfo.getAreacode());
            table.setData(list);
            table.setCount(count);
            jsonUtil.setMsg("成功！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        }else{
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
            jsonUtil.setMsg("区域不能为空！");
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil roleFarmCount(Table table, Agrinfo agrinfo) {
        JSONUtil jsonUtil = new JSONUtil();
        if(agrinfo.getAreacode()!=null&&!"".equals(agrinfo.getAreacode())){
            List<Agrinfo> list = agrinfoDao.roleFarmCount(table,agrinfo);
            table.setData(list);
            jsonUtil.setMsg("成功！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        }else{
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
            jsonUtil.setMsg("区域不能为空！");
        }
        return jsonUtil;
    }

    //统计下载
    @Override
    public List<Agrinfo> agriExcel(Table table, Agrinfo agrinfo) {
        return agrinfoDao.agriInfoCount(table,agrinfo);
    }

    @Override
    public List<Agrinfo> userFarmExcel(Table table, Agrinfo agrinfo) {
        return agrinfoDao.farmCount(table,agrinfo);
    }

    @Override
    public List<Agrinfo> roleFarmExcel(Table table, Agrinfo agrinfo) {
        return agrinfoDao.roleFarmCount(table,agrinfo);
    }
}
